﻿/* FrameRater 0.1
** ActionScript 3 frame rate meter with graph
**
** Author: Pierluigi Pesenti
** http://blog.oaxoa.com
**
** Feel free to use or redistribute but please leave this credits
*/

package com.dusk.view {
	
	import com.game.manager.AnimationManager;
	
	import com.dusk.util.MathUtil;
	import com.dusk.util.SystemUtil;
	import com.game.manager.ConfigManager;
	import com.game.manager.GameGadgetManager;
	import com.game.manager.NotificationManager;
	import com.game.manager.SoundManager;
	
	import config.Config;
	
	import flash.events.MouseEvent;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.display.Sprite;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	
	import com.dusk.net.AssetManager;
	
	public class GameInfoReporter extends Sprite {

		private var _timer:Timer;
		private var _fpsText:TextField;
		private var _ramText:TextField;
		private var _tf:TextFormat;
		private var _c:uint=0;
		private var _dropShadow:DropShadowFilter;
		private var _graph:Sprite;
		private var _graphBox:Sprite;
		private var _graphCounter:uint;
		private var _showGraph:Boolean;
		private var _graphColor:uint;
		private var _cover:Sprite;

		public function GameInfoReporter(textColor:uint=0x000000, drawShadow:Boolean=false, showGraph:Boolean=true, graphColor:uint=0xff0000) {

			_showGraph=showGraph;
			_graphColor=graphColor;

			if (_showGraph) {
				initGraph();
			}
			_dropShadow=new DropShadowFilter(1,90,0,1,2,2);
			_tf=new TextFormat();
			_tf.color=textColor;
			_tf.font="微软雅黑";
			_tf.size=11;
			
			_ramText=new TextField();
			_ramText.width = 100;
			_ramText.height = 10;
			_ramText.x=3;
			_ramText.y=0;
			_ramText.autoSize="left";
			_ramText.setTextFormat(_tf);
			_ramText.defaultTextFormat=_tf;
			
			_fpsText=new TextField();
			_fpsText.width=100;
			_fpsText.height=10;
			_fpsText.x=3;
			_fpsText.y=12;
			_fpsText.autoSize="left";
			_fpsText.setTextFormat(_tf);
			_fpsText.defaultTextFormat=_tf;
			
			_cover = new Sprite();
			_cover.graphics.beginFill(0,0);
			_cover.graphics.drawRect(0,0,50,40);
			_cover.x=_cover.y=0;
			_cover.graphics.endFill();
			_cover.addEventListener(MouseEvent.CLICK, this.doCleanRam);
			_cover.buttonMode = true;
			
			if (drawShadow) {
				_ramText.filters=[_dropShadow];
				_fpsText.filters=[_dropShadow];
			}
			addChild(_ramText);
			addChild(_fpsText);
			addChild(_cover);

			_timer=new Timer(1000);
			_timer.addEventListener(TimerEvent.TIMER, onTimer);
			_timer.start();

			addEventListener(Event.ENTER_FRAME, onFrame);
		}
		private function onTimer(event:TimerEvent):void {
			var val:Number=computeTime();
			_fpsText.text="fps: "+Math.floor(val).toString();
			_ramText.text="ram:"+MathUtil.decimal(SystemUtil.getAppMemory(),1).toString() + " mb";
			if (_showGraph) {
				updateGraph(val);
			}
		}
		private function onFrame(event:Event):void {
			_c++;
		}
		public function computeTime():Number {
			var retValue:uint=_c;
			_c=0;
			return retValue - 1;
		}
		public function updateGraph(n:Number):void {
			if (_graphCounter>30) {
				_graph.x--;
			}
			_graphCounter++;
			_graph.graphics.lineTo(_graphCounter, 1+(stage.frameRate-n)/3);
		}
		private function initGraph():void {
			_graphCounter=0;
			_graph=new Sprite();
			_graphBox=new Sprite();
			_graphBox.graphics.beginFill(0xff0000);
			_graphBox.graphics.drawRect(0,0,36,100);
			_graphBox.graphics.endFill();
			_graph.mask=_graphBox;
			_graph.x=_graphBox.x=5;
			_graph.y=_graphBox.y=30;
			_graph.graphics.lineStyle(1, _graphColor);

			addChild(_graph);
			addChild(_graphBox);
		}
		
		private function doCleanRam(param1:MouseEvent):void
		{
			SoundManager.playSound("Select");
			AnimationManager.clearAllCache();
			GameGadgetManager.clearAllCache();
			AssetManager.getIns().deleteAllAssets();
			SoundManager.clearAllCache()
			System.gc();
			Config.getIns().configManager.loadConfig(null);
			NotificationManager.alert("内存清理成功");
		}
	}
}